<?php
$conf = parse_ini_file("include/model-db-connection/configuration.ini");
$mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$stmt = $mysqli->prepare("SELECT * FROM SWISS_MENU ORDER BY CREATED_DATE_TIME ASC;");
$stmt->execute();
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) {
    $parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $parameters);
?>
<div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
        <?php
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            if ($x['MENU_NAME'] == "Home") {
                ?>
                <li class="visible-xs dropdown ">
                    <a href="index.php"><span class="glyphicon glyphicon-play" aria-hidden="true"></span> <?= $x['MENU_NAME'] ?>
                        <ul class="dropdown-menu"></ul>
                    </a>
                </li>
                <?php
            } else if ($x['MENU_NAME'] == "Contact") {
                ?>
                <li class="visible-xs dropdown ">
                    <a href="contact.php">
                        <span class="glyphicon glyphicon-play" aria-hidden="true"></span> <?= $x['MENU_NAME'] ?>
                        <ul class="dropdown-menu"></ul>
                    </a>
                </li> 
                <?php
            } else {
                ?>
                <li class="dropdown ">
                    <a href="product" class="dropdown-toggle" data-toggle="dropdown">
                        <span class="glyphicon glyphicon-play" aria-hidden="true"></span> <?= $x['MENU_NAME'] ?>
                    </a>
                    <ul class="dropdown-menu">
                        <?php
                        $objConnect = mysql_connect($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword']) or die(mysql_error());
                        $objDB = mysql_select_db($conf['dataBaseName']) or die($config['connection_error_msg']);
                        mysql_query($config['msg_encode'], $objConnect);

                        $sqlGetMenuLevel2 = mysql_query("SELECT * FROM SWISS_MENU_LEVEL1 WHERE REF_ID LIKE '" . $x['MENU_ID'] . "' ORDER BY CREATED_DATE_TIME ASC;");
                        while ($menuLevel1 = mysql_fetch_array($sqlGetMenuLevel2)) {
                            ?>
                            <li class="">
                                <a href="product.php?id=<?= $menuLevel1['MENU_ID'] ?>" class="level2">
                                    <span class="glyphicon glyphicon-play" aria-hidden="true"></span> <?= $menuLevel1['MENU_NAME'] ?>
                                </a>
                            </li>
                            <?php
                            $sqlGetMenuLevel3 = mysql_query("SELECT * FROM SWISS_MENU_LEVEL2 WHERE REF_ID LIKE '" . $menuLevel1['MENU_ID'] . "' ORDER BY CREATED_DATE_TIME ASC;");
                            while ($menuLevel2 = mysql_fetch_array($sqlGetMenuLevel3)) {
                                ?>
                                <li class="">
                                    <a href="product.php?id=<?= $menuLevel2['MENU_ID'] ?>" class="level3"><?= $menuLevel2['MENU_NAME'] ?></a>
                                </li>
                                <?php
                            }
                        }
                        ?>
                    </ul>
                </li>
                <?php
            }
        }
        ?>
    </ul>
</div>
<script>
    $(function () {
        voidElement = $('ul.dropdown-menu:empty').parent();
        voidElement.removeClass('dropdown');
        voidElement.find('a').removeClass('dropdown-toggle').removeAttr('data-toggle');
        voidElement.find('b.caret').hide();

        /*Das dropdownmenu soll beim click auf das Child offen bleiben .. */
        $('.dropdown-menu').click(function (event) {
            event.stopPropagation();
        });
    });
</script>
<!--</div>-->